package com.zhengx.wifip2phelper

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.NetworkInfo
import android.net.wifi.WifiManager
import android.net.wifi.p2p.WifiP2pDevice
import android.net.wifi.p2p.WifiP2pGroup
import android.net.wifi.p2p.WifiP2pInfo
import android.net.wifi.p2p.WifiP2pManager

/**
 * name：WifiP2PBroadcastReceiver
 * class: describe
 * author: zhengx
 * create_time: 2018/11/8
 */
class WifiP2PBroadcastReceiver  : BroadcastReceiver {

    var wifiStatusChangeListener : WifiStatusChangeListener ?= null

    var isWifiP2pEnable : Boolean = false
    var isWifiOpened : Boolean = false
    var isScanning : Boolean = false
    var isConnectedDevice : Boolean = false

    var currentWifiP2pInfo : WifiP2pInfo ?= null
    var currentWifiP2pGroup: WifiP2pGroup ?= null
    var currentNetworkInfo: NetworkInfo ?= null

    var thizDevice: WifiP2pDevice ?= null

    constructor() {}

    override fun onReceive(context: Context?, intent: Intent?) {
//        处理wifi直链的状态回调等

        when(intent?.action) {
            /*wifi直连可不可用的通知*/
            WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION -> {
                val state = intent.getIntExtra(WifiP2pManager.EXTRA_WIFI_STATE, -1)

                isWifiP2pEnable = state == WifiP2pManager.WIFI_P2P_STATE_ENABLED

                when(state) {
                    WifiP2pManager.WIFI_P2P_STATE_ENABLED -> {
                        wifiStatusChangeListener?.onWifiP2PEnable()
                    }
                    else -> {
                        wifiStatusChangeListener?.onWifiP2PDisable()
                    }
                }
            }

            /*wifi p2p扫描状态改变(开始或者停止扫描) 的通知*/
            WifiP2pManager.WIFI_P2P_DISCOVERY_CHANGED_ACTION -> {
                val discoveryStatus = intent.getIntExtra(WifiP2pManager.EXTRA_DISCOVERY_STATE,
                        WifiP2pManager.WIFI_P2P_DISCOVERY_STOPPED)

                isScanning = discoveryStatus == WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED

                wifiStatusChangeListener?.onDiscoverChange(
                        discoveryStatus == WifiP2pManager.WIFI_P2P_DISCOVERY_STARTED)
            }

            /*发现其他p2p设备的通知*/
            WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION -> {
                //发现的wifi设备列表改变， 这里调用wifip2pmanager.requestPeers() 请求最新的WiFi列表
                wifiStatusChangeListener?.onRequestPeers()
            }

            /*连接状态改变通知， 判断是否连接，group信息可在networkinfo中找到*/
            WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION -> {
                val networkInfo = intent.getParcelableExtra<NetworkInfo>(WifiP2pManager.EXTRA_NETWORK_INFO)
                val wifiP2pInfo = intent.getParcelableExtra<WifiP2pInfo>(WifiP2pManager.EXTRA_WIFI_P2P_INFO)
                val wifiP2pGroup = intent.getParcelableExtra<WifiP2pGroup>(WifiP2pManager.EXTRA_WIFI_P2P_GROUP)

                currentNetworkInfo = networkInfo
                currentWifiP2pInfo = wifiP2pInfo
                currentWifiP2pGroup = wifiP2pGroup

                isConnectedDevice = networkInfo.isConnected

                if (networkInfo.isConnected) {
                    //连接上wifi设备
                } else {
                    //没连接上设备
                }
                wifiStatusChangeListener?.onConnectStateChange(wifiP2pGroup, wifiP2pInfo, networkInfo)

            }

            /*本机状态改变通知*/
            WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION -> {
                //本机状态改变
                val wifiP2pDevice = intent.getParcelableExtra<WifiP2pDevice>(WifiP2pManager.EXTRA_WIFI_P2P_DEVICE)
                thizDevice = wifiP2pDevice
                wifiStatusChangeListener?.onThisChange(wifiP2pDevice)
            }

            /*wifi打开关闭的通知*/
            WifiManager.WIFI_STATE_CHANGED_ACTION -> {
                //wifi 关闭开启广播
                val nowState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)
                val previous = intent.getIntExtra(WifiManager.EXTRA_PREVIOUS_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN)

                isWifiOpened = nowState == WifiManager.WIFI_STATE_ENABLED

                wifiStatusChangeListener?.onWifiStateChange(previous, nowState)
            }
        }
    }
}